var dt = $('.dataTablesAjax');
var ajax_datatable = [];

//修改奖品状态
function changeStatus(that) {
    var $name = $(that).attr('data-name');
    if (typeof $name == 'undefined') {
        toastr.success('修改失败');
    }
    $.post('/admin/prize/ajaxSetting/', {
        name: $name
    }, function (data) {
        if (data.status == 0) {
            toastr.success('修改成功');
            $(that).parent().prev().html(data.data)
        } else {
            toastr.success('修改失败');
        }
    });
}
$(function () {

    var dts = $('.dataTablesAjax');
    dts.each(function (i, dt) {
        dt = $(dt);
        var $way = dt.attr('data-way');
        ajax_datatable[$way] = dt.DataTable({
            "processing": true,
            "serverSide": true,
            "searching": false,
            "searchDelay": 800,
            "search": {
                "regex": true
            },
            "ajax": {
                'url': '/admin/prize/ajaxIndex'
            },
            "pagingType": "full_numbers",
            "orderCellsTop": true,
            "dom": '<"html5buttons"B>lTfgitp',
            "buttons": [
                {extend: 'copy', title: 'permission'},
                {extend: 'csv', title: 'permission'},
                {extend: 'excel', title: 'permission'},
                {extend: 'pdf', title: 'permission'},
                {
                    extend: 'print',
                    customize: function (win) {
                        $(win.document.body).addClass('white-bg');
                        $(win.document.body).css('font-size', '10px');
                        $(win.document.body).find('table')
                            .addClass('compact')
                            .css('font-size', 'inherit');
                    }
                }
            ],
            "columns": [
                {
                    "data": "name",
                    "name": "name",
                    "orderable": true
                },
                {
                    "data": "total",
                    "name": "total",
                    "orderable": true
                },
                {
                    "data": "left",
                    "name": "left",
                    "orderable": true
                },
                {
                    "data": "status",
                    "name": "status",
                    "orderable": true
                },
                {
                    "data": "actionButton",
                    "name": "actionButton",
                    "type": "html",
                    "orderable": false
                }
            ],
            "drawCallback": function (settings) {
                ajax_datatable[$way].$('.tooltips').tooltip({
                    placement: 'top',
                    html: true
                });
            },
            "language": {
                url: '/admin/i18n'
            }
        });
    });

    var addPrizesOkBtn = $('.add-prizes-ok-btn');
    addPrizesOkBtn.click(function () {
        var $form = $(this).parent().prev().children('form');
        var $modal = $(this).parents('.add-prizes-modal');
        var $way = $(this).attr('data-way');
        $(this).attr('disabled', 'disabled');
        $(this).text('正在添加，请稍后...');
        $.ajax({
            url: '/admin/prize/batchStore/',
            type: 'POST',
            cache: false,
            data: new FormData($form[0]),
            processData: false,
            contentType: false
        }).done(function (res) {
            toastr.success('添加成功！');
            $modal.modal('hide');
            addPrizesOkBtn.attr('disabled', false);
            addPrizesOkBtn.text('确定');
            ajax_datatable[$way].ajax.reload();
            console.log(res);
        }).fail(function (res) {
            toastr.error('添加出错，请刷新重试！');
        });
    });
});
